package com.zlq.cn.service.un;

import cn.hutool.core.io.FileUtil;
import com.zlq.cn.entity.ZhChArticle;
import com.zlq.cn.mapper.un.ZhChArticleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.cn.util.StringUtils;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 中英文章 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2024-06-02
 */
@Service
public class ZhChArticleService extends ServiceImpl<ZhChArticleMapper, ZhChArticle> {

    @Resource
    private ConfigService configService;

    public static void main(String[] args) {
        Document document = Jsoup.parse(FileUtil.readString(new File("C:\\Users\\14988\\Desktop\\zuowen.txt"), "UTF-8"));
        for (Element element : document.select("#js_content a")) {
            System.out.println(element);
            break;
        }
    }

    @SneakyThrows
    public void pull() {
        String baseUrl = "https://res.wx.qq.com/voice/getvoice?mediaid=";
        Document document = Jsoup.parse(FileUtil.readString(new File("C:\\Users\\14988\\Desktop\\zuowen.txt"), "UTF-8"));
        for (Element element : document.select("#js_content a")) {
            String href = element.attr("href");
            System.out.println(href);
            ZhChArticle one = super.lambdaQuery()
                    .eq(ZhChArticle::getPageUrl, href)
                    .one();
            if (one != null) {
                continue;
            }
            Document documentDtl = Jsoup.connect(href).get();
//            Document documentDtl = Jsoup.parse(FileUtil.readString(new File("C:\\Users\\14988\\Desktop\\zuowen2.html"), "UTF-8"));
            Element eleJsContent = documentDtl.selectFirst("#js_content");
            ZhChArticle zhChArticle = new ZhChArticle();
            System.out.println(href);
            Elements pEles = eleJsContent.select("p");
            int index = 0;
            String enTitle = StringUtils.substringBetween(element.text(), "《", "》");
            if (StringUtils.isBlank(enTitle)) {
                enTitle = StringUtils.substringAfter(element.text(), "《");
            }
            if (StringUtils.isBlank(enTitle)) {
                enTitle = element.text();
            }
            System.out.println(enTitle);
            for (int i = 0; i < pEles.size(); i++) {
                if (enTitle.equals(pEles.get(i).text())) {
                    index = i;
                }
            }
//            zhChArticle.setZhTitle(pEles.get(index + 2).text());
            zhChArticle.setZhContent(pEles.get(index + 2).text());
            zhChArticle.setEnTitle(enTitle);
            zhChArticle.setEnContent(pEles.get(index + 1).text());
            zhChArticle.setPageUrl(href);
            Element mpvoice = eleJsContent.selectFirst("mpvoice");
            if (mpvoice != null) {
                String voiceEncodeFileid = mpvoice.attr("voice_encode_fileid");
                zhChArticle.setAudioUrl(baseUrl + voiceEncodeFileid);
                zhChArticle.setAudioUrl(configService.putObject("zhcnarticle/", voiceEncodeFileid + ".mp3", zhChArticle.getAudioUrl()));
            }
            zhChArticle.setCreateTime(LocalDateTime.now());
            super.save(zhChArticle);
        }
    }
}
